<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="cross" @click="$router.push('/my')" />
    </van-nav-bar>
    <div>
      <van-form @submit="onSubmit" ref="loginForm" validate-trigger="onSubmit">
        <van-field
          type="number"
          v-model="user.mobile"
          name="mobile"
          placeholder="请输入手机号"
          maxlength="11"
          :rules="mobileRules"
        >
          <template #left-icon>
            <div class="iconfont icon-shouji"></div>
          </template>
        </van-field>
        <van-field
          maxlength="6"
          v-model="user.code"
          name="code"
          placeholder="请输入验证码"
          :rules="codeRules"
        >
          <template #left-icon>
            <div class="iconfont icon-yanzhengma"></div>
          </template>
          <template #button>
            <div v-if="cutDownShow" class="msg">
              <van-count-down
                @finish="cutDownShow = false"
                :time="60 * 1000"
                format="ss s"
              />
            </div>
            <div v-else class="msg">
              <van-button
                round
                class="send-sms-btn"
                size="small"
                type="default"
                native-type="button"
                @click="onSendMsg"
                >发送验证码</van-button
              >
            </div>
          </template>
        </van-field>
        <div class="login-btn-wrap">
          <van-button class="login-btn" block type="info" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
// 导入封装好的登录接口函数
import { login, send } from '@/api/login.js'
export default {
  name: 'login',
  data() {
    return {
      user: {
        mobile: '',
        code: ''
      },
      cutDownShow: false,
      // 表单验证规则(用于组件rules属性)
      mobileRules: [
        {
          pattern:
            /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
          message: '手机号码格式错误'
        }
      ],
      codeRules: [
        {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }
      ]
    }
  },
  methods: {
    // 登录函数
    async onSubmit() {
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0 // 持续时间，默认 2000，0 表示持续展示不关闭
      })
      const user = this.user
      try {
        const { data } = await login(user)
        this.$toast.success('登录成功')
        this.$store.commit('setUser', data.data)
        this.$router.push('/my') // 登录成功切换路由
      } catch (error) {
        if (error.response.status === 403 || error.response.status === 400) {
          this.$toast.fail('手机号或验证码错误')
        } else {
          this.$toast.fail('登录失败请稍后再试')
        }
      }
    },
    async onSendMsg() {
      // 1. 校验手机号
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return this.$toast.fail('手机号格式错误')
      }
      this.cutDownShow = true
      try {
        await send(this.user.mobile)
        this.$toast.success('获取验证码成功')
      } catch (error) {
        this.cutDownShow = false
        if (error.response.status === 404) {
          this.$toast.fail('手机号格式错误')
        }
        this.$toast.fail('获取失败请稍后再试')
      }
      // 2. 验证通过，显示倒计时
      // 3. 请求发送验证码
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .iconfont {
    font-size: 37px;
  }
  .van-icon {
    color: #fff;
  }
  .send-sms-btn {
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    color: #666;
    font-size: 22px;
  }
  .msg {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 156px;
    height: 56px;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
